package com.yaocw.baseproject.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.yaocw.baseproject.R;
import com.yaocw.baseproject.entity.HomeBannerEntity;
import com.yaocw.baseproject.entity.HomeEntity;
import com.yaocw.baseproject.entity.HomeListItemEntity;
import com.yaocw.baseproject.entity.HomeMenuEntity;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;

public class HomeAdapter extends RecyclerView.Adapter {
    private final Context mContext;
    private ArrayList<HomeEntity> mDatas;


    public HomeAdapter(Context mContext, ArrayList<HomeEntity> mDatas) {
        this.mContext = mContext;
        this.mDatas = mDatas;
    }

    @Override
    public int getItemViewType(int position) {
        return mDatas.get(position).getViewType();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 1) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.home_banner, parent, false);
            return new HomeBannerViewHolder(view);
        } else if (viewType == 2) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.home_menus, parent, false);
            return new HomeMenusViewHolder(view);
        } else if (viewType == 3) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.home_list, parent, false);
            return new HomeListViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == 1) {
            HomeBannerViewHolder vh = (HomeBannerViewHolder) holder;
        } else if (getItemViewType(position) == 2) {
            HomeMenusViewHolder vh = (HomeMenusViewHolder) holder;
        } else if (getItemViewType(position) == 3) {
            HomeListViewHolder vh = (HomeListViewHolder) holder;
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public class HomeBannerViewHolder extends RecyclerView.ViewHolder {
        private Banner banner;

        public HomeBannerViewHolder(@NonNull View itemView) {
            super(itemView);

            banner = itemView.findViewById(R.id.home_banner);
            banner.setAdapter(new HomeBannerAdapter(HomeBannerEntity.getTestData())).addBannerLifecycleObserver((LifecycleOwner) mContext).setIndicator(new CircleIndicator(mContext));

        }
    }

    public class HomeMenusViewHolder extends RecyclerView.ViewHolder {
        private RecyclerView mRecyclerView;

        public HomeMenusViewHolder(@NonNull View itemView) {
            super(itemView);

            mRecyclerView = itemView.findViewById(R.id.recycler_view);

            final StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.VERTICAL);
            layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
            mRecyclerView.setLayoutManager(layoutManager);

            ArrayList<HomeMenuEntity> bannerEntities = new ArrayList<HomeMenuEntity>();
            bannerEntities.add(new HomeMenuEntity("首页", R.mipmap.logo));
            bannerEntities.add(new HomeMenuEntity("新闻", R.mipmap.logo));
            bannerEntities.add(new HomeMenuEntity("新闻", R.mipmap.logo));
            bannerEntities.add(new HomeMenuEntity("新闻", R.mipmap.logo));
            bannerEntities.add(new HomeMenuEntity("新闻", R.mipmap.logo));
            bannerEntities.add(new HomeMenuEntity("新闻", R.mipmap.logo));
            bannerEntities.add(new HomeMenuEntity("其他", R.mipmap.logo));
            HomeMenusAdapter adapter = new HomeMenusAdapter(mContext, bannerEntities);
            mRecyclerView.setAdapter(adapter);

        }
    }

    public class HomeListViewHolder extends RecyclerView.ViewHolder {
        private RecyclerView mRecyclerView;

        public HomeListViewHolder(@NonNull View itemView) {
            super(itemView);

            mRecyclerView = itemView.findViewById(R.id.recycler_view);

            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            mRecyclerView.setLayoutManager(linearLayoutManager);

            ArrayList<HomeListItemEntity> list = new ArrayList<HomeListItemEntity>();
            list.add(new HomeListItemEntity("关关雎鸠，在河之洲。窈窕淑女，君子好逑。", R.mipmap.logo));
            list.add(new HomeListItemEntity("参差荇菜，左右流之。窈窕淑女，寤寐求之。", R.mipmap.logo));
            list.add(new HomeListItemEntity("关关和鸣的雎鸠，栖息在河中的小洲。贤良美好的女子", R.mipmap.logo));
            list.add(new HomeListItemEntity("追求却没法得到，日日夜夜总思念她。绵绵不断的思念，叫人翻来覆去难入睡。", R.mipmap.logo));
            list.add(new HomeListItemEntity("求之不得，寤寐思服。悠哉悠哉，辗转反侧。", R.mipmap.logo));
            list.add(new HomeListItemEntity("这首短小的诗篇，在中国文学史上占据着特殊的位置。它是《诗经》的第一篇，而《诗经》是中国文学最古老的典籍", R.mipmap.logo));
            list.add(new HomeListItemEntity("关关雎鸠，在河之洲。窈窕淑女，君子好逑。", R.mipmap.logo));
            list.add(new HomeListItemEntity("参差荇菜，左右流之。窈窕淑女，寤寐求之。", R.mipmap.logo));
            list.add(new HomeListItemEntity("关关和鸣的雎鸠，栖息在河中的小洲。贤良美好的女子", R.mipmap.logo));
            list.add(new HomeListItemEntity("追求却没法得到，日日夜夜总思念她。绵绵不断的思念，叫人翻来覆去难入睡。", R.mipmap.logo));
            list.add(new HomeListItemEntity("求之不得，寤寐思服。悠哉悠哉，辗转反侧。", R.mipmap.logo));
            list.add(new HomeListItemEntity("这首短小的诗篇，在中国文学史上占据着特殊的位置。它是《诗经》的第一篇，而《诗经》是中国文学最古老的典籍", R.mipmap.logo));
            list.add(new HomeListItemEntity("关关雎鸠，在河之洲。窈窕淑女，君子好逑。", R.mipmap.logo));
            list.add(new HomeListItemEntity("参差荇菜，左右流之。窈窕淑女，寤寐求之。", R.mipmap.logo));
            list.add(new HomeListItemEntity("关关和鸣的雎鸠，栖息在河中的小洲。贤良美好的女子", R.mipmap.logo));
            list.add(new HomeListItemEntity("追求却没法得到，日日夜夜总思念她。绵绵不断的思念，叫人翻来覆去难入睡。", R.mipmap.logo));
            list.add(new HomeListItemEntity("求之不得，寤寐思服。悠哉悠哉，辗转反侧。", R.mipmap.logo));
            list.add(new HomeListItemEntity("这首短小的诗篇，在中国文学史上占据着特殊的位置。它是《诗经》的第一篇，而《诗经》是中国文学最古老的典籍", R.mipmap.logo));
            HomeListAdapter adapter = new HomeListAdapter(mContext, list);
            mRecyclerView.setAdapter(adapter);

        }
    }
}
